cc.Class({
  extends: cc.Component,
  
  properties: {
    toggleSprite: cc.Sprite,
    onSpriteFrame: cc.SpriteFrame,
    offSpriteFrame: cc.SpriteFrame,
    _isChecked: true,
    isChecked: {
      get: function () {
        return this._isChecked;
      },
      set: function (value) {
        this._isChecked = value;
        this._updateToggle();
      }
    },
    checkEvents: {
      default: [],
      type: cc.Component.EventHandler
    },
  },
  onLoad() {
    this._updateToggle();
  },
  
  onEnable() {
    this.toggleSprite.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
    
  },
  onDisable() {
    this.toggleSprite.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
  },
  _onTouchEnd(event) {
    event.stopPropagation();
    this.isChecked = !this.isChecked;
    if (this.checkEvents) {
      cc.Component.EventHandler.emitEvents(this.checkEvents, this);
    }
    this._updateToggle();
  },
  
  _updateToggle() {
    this.toggleSprite.spriteFrame = this._isChecked ? this.onSpriteFrame : this.offSpriteFrame;
  }
});

